package cn.edu.sjtu.se.peakfactory.dao.impls.hibernate;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import cn.edu.sjtu.se.lchang.socialnetwork.dao.impls.hibernate.BaseDAOImpl;
import cn.edu.sjtu.se.peakfactory.dao.bean.GameProfile;
import cn.edu.sjtu.se.peakfactory.dao.interfaces.GameProfileDAO;

public class GameProfileDAOImpl extends BaseDAOImpl<GameProfile> implements GameProfileDAO {

	@Override
	public GameProfile getProfileByGameUser(Long gameId, Long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(GameProfile.class);
		criteria.add(Restrictions.eq("game.id", gameId)).add(Restrictions.eq("user.id", id));
		List<GameProfile> profiles = findByCriteria(criteria);
		if (profiles != null && !profiles.isEmpty()) {
			return profiles.get(0);
		}
		return null;
	}

	@Override
	public List<GameProfile> getProfilesByUser(Long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(GameProfile.class);
		criteria.add(Restrictions.eq("user.id", id)).addOrder(Order.desc("registerTime"));
		return findByCriteria(criteria);
	}

}
